package com.lancoo.answer.view.fragment.option

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.lancoo.answer.R
import com.lancoo.answer.component.OptionComponent
import com.lancoo.answer.databinding.EvFragmentSingleBinding
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.bean.EvOptionBean
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.view.base.BaseVMFragment
import org.greenrobot.eventbus.EventBus

/**
 * Created by Dadong on 2021/12/15
 * Describe: 单选题和完形填空的作答
 */
class SingleOptionFragment : BaseVMFragment<EvFragmentSingleBinding>() {

    private lateinit var ques: Ques
    private var typeIndex = 0
    private var quesIndex = 0
    private var childIndex = 0
    private var defaultLoadIndex = 0
    private var multiable = false
    private var deepable = true
    private var tvChildVisible = View.VISIBLE

    private var chooseCallback: ChooseCallback? = null

    companion object {
        fun loadFragment(typeIndex: Int, quesIndex: Int, defaultLoadIndex: Int): Fragment? {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt("loadQuesIndex", defaultLoadIndex)
            val singleOptionFragment = SingleOptionFragment()
            singleOptionFragment.arguments = mBundle
            return singleOptionFragment
        }
    }

    override fun initView(view: View) {
//        EventBus.getDefault().register(this)
    }

    override fun initData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        defaultLoadIndex = requireArguments().getInt("loadQuesIndex", 0)
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        mBinding.optionComponent.setChild(ques.ChildList!![childIndex])
        mBinding.optionComponent.setChildIndex(childIndex)
        mBinding.optionComponent.setChooseColorDeep(deepable)
        mBinding.optionComponent.enableMultiable(multiable)
        mBinding.optionComponent.setTvChildVisibility(tvChildVisible)
        if (ques.GenreID == "0") {
            mBinding.optionComponent.setTvChildVisibility(View.GONE)
        }
        mBinding.optionComponent.setCallback(object : OptionComponent.OptionCallback {
            override fun clickCallback(item: EvOptionBean) {
                if (chooseCallback != null) {
                    chooseCallback!!.clickCallback(item)
                } else if (ques.GenreID != "0") { //genreId == 0 适配阅读综合题的匹配加简答或者匹配加选择
                    ques.ChildList!![0].ItemList!![0].StuAnswer = item.OptionIndex
                }
                if (ques.GenreID == "15" || ques.GenreID == "17" || ques.GenreID == "0") { //双组
                    updateCheck()
                }
            }
        })
    }

    fun setChildIndex(childIndex: Int) {
        this.childIndex = childIndex
    }

    override fun getLayoutId(): Int {
        return R.layout.ev_fragment_single
    }

    fun setChooseCallback(chooseCallback: ChooseCallback) {
        this.chooseCallback = chooseCallback
    }

    fun setMultiable(enable: Boolean) {
        this.multiable = enable
    }

    fun setDeepable(enable: Boolean) {
        this.deepable = enable
    }

    fun setTvChildVisibility(visible: Int) {
        tvChildVisible = visible
    }

    fun updateCheck() {
        mBinding.optionComponent.resetState()
        if (ques.SpecialFeatureCodes == "47|101") { //高职匹配题
            ques.ChildList!!.forEach {
                it.ItemList!!.forEach {
                    if (!it.StuAnswer.isNullOrEmpty())
                        mBinding.optionComponent.updateCheck(it.StuAnswer!!)
                }
            }
        } else {
            ques.ChildList!![childIndex].ItemList!!.forEach {
                if (!it.StuAnswer.isNullOrEmpty())
                    mBinding.optionComponent.updateCheck(it.StuAnswer!!)
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
//        EventBus.getDefault().unregister(this)
    }

    fun enableAnswer(enable: Boolean) {
        mBinding.optionComponent.enableAnswer(enable)
    }

    interface ChooseCallback {
        fun clickCallback(item: EvOptionBean)
    }

    fun adjustFontSize() {
        mBinding.optionComponent.adjustFontSize()
    }

    fun markQuestion(){
        mBinding.optionComponent.markQuestion()
    }
}
